package com.nosweattechnet.mapmyway;
import java.io.IOException;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
import com.nosweattechnet.datatypes.Place;
import com.nosweattechnet.placemap.PlaceMapActivity;
import com.nosweattechnet.server.AddPlaceListAsyncTask;

@SuppressWarnings("rawtypes")
public class MapItemizedOverlay extends ItemizedOverlay {
	
	// stores OverLay in list
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private ArrayList<Place> mPlaces = new ArrayList<Place>();
	private String userid;
	private Context mContext;
	
	public MapItemizedOverlay(Drawable defaultMarker, Context context,String user_id) {
		  super(boundCenterBottom(defaultMarker));
		  mContext = context;
		  userid=user_id;
		  populate();
	}


	// add overlay to ArrayList and prepares them to be drawn
	public void addOverlay(OverlayItem overlay,Place p) {
	    mOverlays.add(overlay);
	    mPlaces.add(p);
	    setLastFocusedIndex(-1);
	    populate();
	}
	
	public void clearPlaces(){
		mOverlays.clear();
		mPlaces.clear();
		setLastFocusedIndex(-1);
		populate();
	}

	// override createItem method
	// get overlay item
	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	// override size method and returns size of list
	@Override
	public int size() {
		return mOverlays.size();
	}
	
	// override ontap method
	// on tap display alert dialog message
	@Override
	protected boolean onTap(int index) {
	  OverlayItem item = mOverlays.get(index);
	  final Place p=mPlaces.get(index);
	  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
	  dialog.setTitle(item.getTitle());
	  dialog.setMessage(item.getSnippet());
	  dialog.setPositiveButton("Go", new DialogInterface.OnClickListener() { 
		  // button on click go to place map information
	      public void onClick(DialogInterface dialog, int id) { 
	    	  Intent intent = new Intent(mContext, PlaceMapActivity.class);
	    	  intent.putExtra(mContext.getString(R.string.place_id_intent),p.getId());
	    	  intent.putExtra(mContext.getString(R.string.user_id_intent),userid);
	    	  // View the map of the location selected
	    	  mContext.startActivity(intent);
	    }
	  });
	  dialog.setNeutralButton("Add to Favorites", new DialogInterface.OnClickListener() {
	      public void onClick(DialogInterface dialog, int id) { 
	    	  AddPlaceListAsyncTask aplat=new AddPlaceListAsyncTask(p.getId(),userid);
	    	  aplat.execute();
	    	  // Writes a place to internal memory
	    	  try {
				Storage.writePlaceToStorage(mContext, p);
	    	  } catch (IOException e) {
				e.printStackTrace();
	    	  }
	    }
	  });
	  dialog.setNegativeButton("Report", new DialogInterface.OnClickListener() {
		  public void onClick(DialogInterface dialog, int id){
			  Intent intent = new Intent(mContext, ReportPlace.class);
			  intent.putExtra(mContext.getString(R.string.place_id_intent),p.getId());
			  intent.putExtra(mContext.getString(R.string.name_of_place),p.getName());
			  intent.putExtra(mContext.getString(R.string.user_id_intent),userid);
			  mContext.startActivity(intent);
		  }
	  });

	  dialog.show();
	  return true;
	}
}
